package org.vectrics.common.http;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


public class SaveStaticContent 
{
	private transient static Logger log = Logger.getLogger(SaveStaticContent.class);

	public SaveStaticContent() {
		super();
	}

	public static void main(String[] args) {
	    PropertyConfigurator.configure("log4j.properties");
	
	    SaveStaticContent saveStaticContent = new SaveStaticContent();
	    saveStaticContent.execute();
	    
        log.info("finished");
        
        System.exit(-1);
	}

	protected Collection getDefinitions() {
		String localRootDir = "/home/mmoore/apps/jboss-flashrecruit/server/default/deploy/jbossweb-tomcat55.sar/ROOT.war";
		Collection definitions = new ArrayList();
		
		StaticSnapshotDef def = new StaticSnapshotDef();
		def.setHost("www.hexacta.com");
		def.setHostDirectory("site/main/index.do");
		def.setLocalPath(localRootDir);
		def.setLocalFileName("index.html");
		definitions.add(def);

		def = new StaticSnapshotDef();
		def.setHost("www.hexacta.com");
		def.setHostDirectory("site/main/product/recruit/demo.do");
		def.setLocalPath(localRootDir);
		def.setLocalFileName("demo.html");
		definitions.add(def);

		def = new StaticSnapshotDef();
		def.setHost("www.hexacta.com");
		def.setHostDirectory("site/main/product/recruit/index.do");
		def.setLocalPath(localRootDir);
		def.setLocalFileName("flashrecruit.html");
		definitions.add(def);

		def = new StaticSnapshotDef();
		def.setHost("www.hexacta.com");
		def.setHostDirectory("site/main/product/recruit/forums.do");
		def.setLocalPath(localRootDir);
		def.setLocalFileName("forum.html");
		definitions.add(def);

		def = new StaticSnapshotDef();
		def.setHost("www.hexacta.com");
		def.setHostDirectory("site/main/company/contact.do");
		def.setLocalPath(localRootDir);
		def.setLocalFileName("contact.html");
		definitions.add(def);
			

		def = new StaticSnapshotDef();
		def.setHost("www.hexacta.com");
		def.setHostDirectory("support/main/support/index.do");
		def.setLocalPath(localRootDir);
		def.setLocalFileName("support.html");
		definitions.add(def);

		def = new StaticSnapshotDef();
		def.setHost("www.hexacta.com");
		def.setHostDirectory("site/main/product/recruit/download.do");
		def.setLocalPath(localRootDir);
		def.setLocalFileName("download.html");
		definitions.add(def);
		
		

		def = new StaticSnapshotDef();
		def.setHost("www.hexacta.com");
		def.setHostDirectory("site/main/company/about.do");
		def.setLocalPath(localRootDir);
		def.setLocalFileName("about.html");
		definitions.add(def);
		
		
		

		def = new StaticSnapshotDef();
		def.setHost("www.hexacta.com");
		def.setHostDirectory("site/main/product/recruit/license.do");
		def.setLocalPath(localRootDir);
		def.setLocalFileName("license.html");
		definitions.add(def);

			
		return(definitions);
		
	}
	
	
	public void execute() {
		Iterator iter = this.getDefinitions().iterator();
		while (iter.hasNext()) {
			StaticSnapshotDef def = (StaticSnapshotDef)iter.next();
			this.executeDefinition(def);
		}
	}
	
	public void executeDefinition(StaticSnapshotDef definition) {
		GetMethod httpGet = null;
		try {
			HttpClient client = new HttpClient();
			client.setConnectionTimeout(10000);

			String urlStr = "http://" + definition.getHost() + "/" + definition.getHostDirectory();
			httpGet = new GetMethod(urlStr);
			int responseCode = client.executeMethod(httpGet);
			InputStream is = httpGet.getResponseBodyAsStream();
			
			String localPathAndFile = definition.getLocalPath() + "/" + definition.getLocalFileName();
			FileOutputStream fileOut = new FileOutputStream(localPathAndFile);
			boolean done = false;
			while (!done) {
				int c = is.read();
				if (c >= 0) {
					fileOut.write(c);
				} else {
					done = true;
				}
			}
			fileOut.close();
			is.close();
		} catch (IOException ex) {
			String errorMsg = "IOException while communicating with server.  Address= " + definition.getHost() + " Message = " +  ex.getMessage();
			log.error(errorMsg, ex);
		} finally {
			httpGet.releaseConnection();
		}
		
	}


	

}
